<#import "/spring.ftl" as spring />
<#import "/inspector-time-macros.ftl" as imacro />

<html>
<head>
	<title>addScheduleRule.ftl</title>
	
	<script language="javascript">
		<!--
		
		function get(id) { return document.getElementById(id); }
		function setStyleAttr(id,p,v) { var e = get(id);if(e != null) { e.style[p] = v; } }
		function show(id) { var e = get(id);if (e != null) { if(arguments.length>1) { setStyleAttr(id,"display",arguments[1]); } else { setStyleAttr(id,'display','block'); } } }
		function hide(id) { var e = get(id); if (e != null) { if(arguments.length>1) { setStyleAttr(id,"display",arguments[1]); } else { setStyleAttr(id,'display','none'); } } }
		
		function showIndexes(which) {
			if (which == 'DAY_OF_WEEK') {
				show('daysOfWeek');
				hide('dates')
			} else if (which == 'SINGLE_DAY'){
				show('dates');
				hide('daysOfWeek');
			} else {
				hide('dates');
				hide('daysOfWeek');
			}
		}
		
		function init() {
			showIndexes('${command.frequencyType!}');;
		}
		
		window.onload=init;
		-->
	</script>
</head>

<body>

<div id="content">

	<#if command.id??>
		<#assign mode="edit" />
	<#else>
		<#assign mode="add" />
	</#if>
	
	<#if mode == "edit">
		<h1>EDIT AVAILABILITY RULE</h1>
		
		<p>Your current rule details:</p>
		
		<p>
		<#-- assign a new var name so we can use the common "details" include -->
		<#assign scheduleRule = command />
		<#include "/inspector/scheduleRule/include-scheduleRuleDetails.ftl" />
		</p>
		
		<p>
		Editing a rule will modify ALL future timeslots based on this rule. 
		If you would like to modify a single timeslot, just <a href="addScheduleRule.html?inspectorId=${RequestParameters.inspectorId}">add</a> a new non-recurring rule.
		</p>
	<#else>
		<h1>ADD AVAILABILITY RULE</h1>
	</#if>
	
	<@imacro.listErrors "command" />
	
	<form method="post" method="addScheduleRule.html">
	<#if mode="edit">
		<@spring.formHiddenInput "command.id" />
	</#if>
	<div class="databoxWrapper">
		<div class="databox">
		<h1>YOUR RULE</h1>
			<table class="databoxLabelTable2Cols">
			<tr>
				<th>RULE TYPE:</td>
				<td>
					<@spring.bind "command.frequencyType" />
					<input type="radio" name="${spring.status.expression}" value="DAY_OF_WEEK" class="radio"
						<#if spring.stringStatusValue == "DAY_OF_WEEK">checked="checked"</#if> onclick="showIndexes('DAY_OF_WEEK')"/> RECURRING
						
					&nbsp;&nbsp;
					
					<input type="radio" name="${spring.status.expression}" value="SINGLE_DAY" class="radio"
						<#if spring.stringStatusValue == "SINGLE_DAY">checked="checked"</#if> onclick="showIndexes('SINGLE_DAY')"/> NON-RECURRING
						
					<!-- Begin days of Week checkboxes -->
					<div id="daysOfWeek" style="margin:20px;">
					
						<p>Please select which days of week to apply this rule:</p>
		
						<#-- couldn't make the spring checkbox tag work with Integers in the hashmap,
							so have to do it by hand -->
						<#assign keys = daysOfWeek?keys>
						<@spring.bind "command.units" /> 
						<ul style="list-style-type: none;">
							<#list keys as key>
								<li style="padding: 2px;"><input type="checkbox" name="${spring.status.expression}" value="${key}" class="checkbox"
								<#list command.units as ruleUnit>
									<#if ruleUnit?string == key>checked</#if>
								</#list>
								> ${daysOfWeek[key]}
								
							</#list>			
						</ul>
					</div>
					<!-- End days of Week checkboxes -->
					
					<!-- Begin date fields -->
					<div id="dates" style="margin:20px;">
						<p>Please enter a start and end date for this rule. You can leave End Date blank for rules that should not end.</p>
					
						<b>START DATE:</b> <@spring.formInput "command.startDate" />
						<br /><br />
						<b>END DATE:</b> <@spring.formInput "command.endDate" /> (MM/dd/yyyy)
					</div>
					<!-- End date fields -->
					
				</td>
			</tr>
			<tr>
				<th>START TIME:</td>
				<td class="databoxFreeForm"><@spring.formSingleSelect "command.startHour", hours/>:<@spring.formSingleSelect "command.startMinute", minutes/></td>
			</tr>
			<tr>
				<th>END TIME:</td>
				<td class="databoxFreeForm"><@spring.formSingleSelect "command.endHour", hours/>:<@spring.formSingleSelect "command.endMinute", minutes/></td>
			</tr>
			<tr>
				<th>ARE YOU AVAILABLE IN THIS TIMESLOT?</td>
				<td><@imacro.singleRadioButton "command.available"," YES" " NO" "&nbsp;&nbsp;" "class=radio"/></td>
			</tr>
		</table>
		</div>
	</div>
	
	<#if mode == "edit">
	    <input type="submit" value="SAVE RULE" class="formButton"/>&nbsp;&nbsp;&nbsp;<a href="<@spring.url "/inspector/deleteScheduleRule.html?inspectorId=${RequestParameters.inspectorId}&ruleId=${command.id}" />" class="formLink">-DELETE RULE-</a>&nbsp;&nbsp;&nbsp;<a href="viewScheduleRules.html?inspectorId=${RequestParameters.inspectorId}" class="formLink">-CANCEL-</a>
	    
	<#else>
		<input type="submit" value="ADD RULE" class="formButton"/>
	</#if>
	
	</form>

</div>
</body>
</html>